home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / borland / prbgi097.zip / BGIDEMO.INC < prev    next >
Text File  |  1992-12-15  |  38KB  |  1,255 lines

  1. #include <STDio.h>
  2. #include <CONIO.h>
  3. #include <math.h>
  4. #include <DOS.h>
  5.  
  6. #ifdef _cplusplus
  7.    #define min(a,b)    (((a) < (b)) ? (a) : (b))
  8. #endif
  9.  
  10. #define TRUE   1        /* Define some handy constants   */
  11. #define FALSE  0        /* Define some handy constants   */
  12. #define PI  3.14159     /* Define a value for PI   */
  13. #define ON  1        /* Define some handy constants   */
  14. #define OFF 0        /* Define some handy constants   */
  15.  
  16. char *Fonts[] = {
  17.   "DefaultFont",   "TriplexFont",   "SmallFont",
  18.   "SansSerifFont", "GothicFont"
  19. };
  20.  
  21. char *LineStyles[] = {
  22.   "SolidLn",  "DottedLn",  "CenterLn",  "DashedLn",  "UserBitLn"
  23. };
  24.  
  25. char *FillStyles[] = {
  26.   "EmptyFill",  "SolidFill",      "LineFill",      "LtSlashFill",
  27.   "SlashFill",  "BkSlashFill",    "LtBkSlashFill", "HatchFill",
  28.   "XHatchFill", "InterleaveFill", "WideDotFill",   "CloseDotFill"
  29. };
  30.  
  31. char *TextDirect[] = {
  32.   "HorizDir",  "VertDir"
  33. };
  34.  
  35. char *HorizJust[] = {
  36.   "LeftText",   "CenterText",   "RightText"
  37. };
  38.  
  39. char *VertJust[] = {
  40.   "BottomText",  "CenterText",  "TopText"
  41. };
  42.  
  43. struct PTS {
  44.   int x, y;
  45. }; /* Structure to hold vertex points  */
  46.  
  47. int   xasp_,yasp_;
  48. int    ErrorCode;    /* Reports any graphics errors      */
  49.  
  50.  
  51.  
  52. /*                         */
  53. /* REPORTSTATUS: Report the current configuration of the system   */
  54. /* after the auto-detect initialization.           */
  55. /*                         */
  56.  
  57. int ReportStatus(void far * GraphDriverPtr)
  58. {
  59.   struct viewporttype     viewinfo; /* Params for inquiry procedures*/
  60.   struct linesettingstype lineinfo;
  61.   struct fillsettingstype fillinfo;
  62.   struct textsettingstype textinfo;
  63.   struct palettetype   palette;
  64.  
  65.   const char *driver, *mode;        /* Strings for driver and mode   */
  66.   int x, y;
  67.   int xRes,yRes;
  68.  
  69.   getviewsettings( &viewinfo );
  70.   getlinesettings( &lineinfo );
  71.   getfillsettings( &fillinfo );
  72.   gettextsettings( &textinfo );
  73.   getpalette( &palette );
  74.  
  75.   x = 10;
  76.   y = 4;
  77.  
  78.   MainWindow( "Status report after InitGraph" );
  79.   settextjustify( LEFT_TEXT, TOP_TEXT );
  80.  
  81.   driver = getdrivername();
  82.   mode = BGI_getmodename(BGI_getgraphmode(0)); /* get current setting     */
  83.  
  84.   gprintf( &x, &y, "Graphics device    : %-20s (%d)", driver, *(int*)GraphDriverPtr );
  85.   gprintf( &x, &y, "Graphics mode      : %-20s (%d)", mode, BGI_getgraphmode(0) );
  86.   gprintf( &x, &y, "Screen resolution  : ( 0, 0, %d, %d )", getmaxx(), getmaxy() );
  87.  
  88.   gprintf( &x, &y, "Current view port  : ( %d, %d, %d, %d )",
  89.   viewinfo.left, viewinfo.top, viewinfo.right, viewinfo.bottom );
  90.   gprintf( &x, &y, "Clipping           : %s", viewinfo.clip ? "ON" : "OFF" );
  91.  
  92.   gprintf( &x, &y, "Current position   : ( %d, %d )", getx(), gety() );
  93.   gprintf( &x, &y, "Colors available   : %d", MaxColors );
  94.   gprintf( &x, &y, "Current color      : %d", getcolor() );
  95.  
  96.   gprintf( &x, &y, "Line style         : %s", LineStyles[ lineinfo.linestyle ] );
  97.   gprintf( &x, &y, "Line thickness     : %d", lineinfo.thickness );
  98.  
  99.   gprintf( &x, &y, "Current fill style : %s", FillStyles[ fillinfo.pattern ] );
  100.   gprintf( &x, &y, "Current fill color : %d", fillinfo.color );
  101.  
  102.   gprintf( &x, &y, "Current font       : %s", Fonts[ textinfo.font ] );
  103.   gprintf( &x, &y, "Text direction     : %s", TextDirect[ textinfo.direction ] );
  104.   gprintf( &x, &y, "Character size     : %d", textinfo.charsize );
  105.   gprintf( &x, &y, "Horizontal justify : %s", HorizJust[ textinfo.horiz ] );
  106.   gprintf( &x, &y, "Vertical justify   : %s", VertJust[ textinfo.vert ] );
  107.  
  108.   if ( BGI_getresolution(&xRes,&yRes) == 0 )
  109.   {
  110.      gprintf( &x, &y, "X resolution [ppi] : %d", xRes );
  111.      gprintf( &x, &y, "Y resolution [ppi] : %d", yRes );
  112.   }
  113.  
  114.   Pause();           /* Pause for user to read screen*/
  115.   return(0);
  116.  
  117. }
  118.  
  119.  
  120.  
  121. int static Textfont;
  122.  
  123. int TextDump2(void far * UnusedPointer)
  124. {
  125.   static int CGASizes[]  = {
  126.     1, 3, 7, 3, 3   };
  127.   static int NormSizes[] = {
  128.     1, 4, 7, 4, 4   };
  129.  
  130.   char buffer[80];
  131.   int ch, wwidth, lwidth, size;
  132.   struct viewporttype vp;
  133.  
  134.     sprintf( buffer, "%s Character Set", Fonts[Textfont] );
  135.     MainWindow( buffer );     /* Display fontname as banner */
  136.     getviewsettings( &vp );      /* read current viewport   */
  137.  
  138.     settextjustify( LEFT_TEXT, TOP_TEXT );
  139.     moveto( 2, 3 );
  140.  
  141.     buffer[1] = '\0';                   /* Terminate string             */
  142.     wwidth = vp.right - vp.left; /* Determine the window width */
  143.     lwidth = textwidth( "H" );          /* Get average letter width     */
  144.  
  145.     if( Textfont == DEFAULT_FONT ){
  146.       changetextstyle( Textfont, HORIZ_DIR, 1 );
  147.       ch = 0;
  148.       while( ch < 256 ){      /* For each possible character   */
  149.    buffer[0] = ch;      /* Put character into a string   */
  150.    outtext( buffer );      /* send string to screen   */
  151.    if( (getx() + lwidth) > wwidth )
  152.      moveto( 2, gety() + textheight("H") + 3 );
  153.    ++ch;          /* Goto the next character */
  154.       }
  155.     }
  156.     else{
  157.  
  158.       size = (MaxY < 200) ? CGASizes[Textfont] : NormSizes[Textfont];
  159.       changetextstyle( Textfont, HORIZ_DIR, size );
  160.  
  161.       ch = '!';                         /* Begin at 1st printable       */
  162.       while( ch < 127 ){      /* For each printable character */
  163.    buffer[0] = ch;      /* Put character into a string   */
  164.    outtext( buffer );      /* send string to screen   */
  165.    if( (lwidth+getx()) > wwidth )   /* Are we still in window? */
  166.      moveto( 2, gety()+textheight("H")+3 );
  167.    ++ch;          /* Goto the next character */
  168.       }
  169.  
  170.     }
  171.  
  172.     Pause();            /* Pause until user acks   */
  173.     return(0);
  174. }  /* end of TextDump2 */
  175.  
  176. /*                         */
  177. /* TEXTDUMP: Display the all the characters in each of the  */
  178. /* available fonts.                 */
  179. /*                         */
  180.  
  181. void TextDump()
  182. {
  183.   for( Textfont=0 ; Textfont<5 ; ++Textfont ){  /* For each available font */
  184.    //DrawAndPrint(TextDump2,NULL);
  185.   }               /* End of FONT loop     */
  186. }
  187.  
  188. /*                         */
  189. /* BAR3DDEMO: Display a 3-D bar chart on the screen.     */
  190. /*                         */
  191.  
  192. int Bar3DDemo(void far * UnusedPointer)
  193. {
  194.   static int barheight[] = {
  195.     1, 3, 5, 4, 3, 2, 1, 5, 4, 2, 3   };
  196.   struct viewporttype vp;
  197.   int xstep, ystep;
  198.   int i, j, h, color, bheight;
  199.   char buffer[10];
  200.  
  201.   MainWindow( "Bar 3-D / Rectangle Demonstration" );
  202.  
  203.   srand(0);
  204.   h = 3 * textheight( "H" );
  205.   getviewsettings( &vp );
  206.   settextjustify( CENTER_TEXT, TOP_TEXT );
  207.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  208.   outtextxy( MaxX/2, 6, "These are 3-D Bars" );
  209.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  210.   setviewport( vp.left+50, vp.top+40, vp.right-50, vp.bottom-10, 1 );
  211.   getviewsettings( &vp );
  212.  
  213.   line( h, h, h, vp.bottom-vp.top-h );
  214.   line( h, (vp.bottom-vp.top)-h, (vp.right-vp.left)-h, (vp.bottom-vp.top)-h );
  215.   xstep = ((vp.right-vp.left) - (2*h)) / 10;
  216.   ystep = ((vp.bottom-vp.top) - (2*h)) / 5;
  217.   j = (vp.bottom-vp.top) - h;
  218.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  219.  
  220.   for( i=0 ; i<6 ; ++i ){
  221.     line( h/2, j, h, j );
  222.     itoa( i, buffer, 10 );
  223.     outtextxy( 0, j, buffer );
  224.     j -= ystep;
  225.   }
  226.  
  227.   j = h;
  228.   settextjustify( CENTER_TEXT, TOP_TEXT );
  229.  
  230.   for( i=0 ; i<11 ; ++i ){
  231.     color = random( MaxColors-1 ) +1;
  232.     setfillstyle( i+1, color );
  233.     line( j, (vp.bottom-vp.top)-h, j, (vp.bottom-vp.top-3)-(h/2) );
  234.     itoa( i, buffer, 10 );
  235.     outtextxy( j, (vp.bottom-vp.top)-(h/2), buffer );
  236.     if( i != 10 ){
  237.       bheight = (vp.bottom-vp.top) - h - 1;
  238.       bar3d( j, (vp.bottom-vp.top-h)-(barheight[i]*ystep), j+xstep, bheight,
  239.              min(getmaxy(),getmaxx())/25, 1 );
  240.     }
  241.     j += xstep;
  242.   }
  243.  
  244.   Pause();           /* Pause for user's response    */
  245.   return(0);
  246.  
  247. }
  248.  
  249. /*                         */
  250. /* RANDOMBARS: Display random bars           */
  251. /*                         */
  252.  
  253. int RandomBars(void far * UnusedPointer)
  254. {
  255.   int color;
  256.   int i;
  257.  
  258.   MainWindow( "Random Bars" );
  259.   /* StatusLine( "Esc aborts or press a key..." ); */
  260.   srand(0);
  261.   for ( i=0; i<10; i++ )
  262.   {
  263.     color = random( MaxColors-1 )+1;
  264.     setcolor( color );
  265.     s